package com.stylefeng.guns.core.util;

import org.lionsoul.ip2region.*;

import java.io.IOException;

public class IpUtil {

    public static void main(String[] args) {
        String ip = "113.116.54.204";
        ip="192.168.0.151";
        String addr = address(ip,0);
        System.out.println(address(ip,2)+":addr1");
        System.out.println(address(ip,3)+":addr2");
    }

    /**
     * 判断是否是ip地址
     * */
    public static boolean isIpAddress(String ip){
        if(ip==null||ip.equals("")){
            return false;
        }
        return  Util.isIpAddress(ip); // true
    }
    /**
     * 获取地址（中国|0|北京|北京市|阿里云）
     * @param ip 地址
     * @param index 0代表获取国家，2代表获取省份，3代表获取城市
     * */
    public static String address(String ip,int index) {
        DataBlock block = null;
        DbSearcher searcher = null;
        DbConfig config = null;
        String dizhi = "";
        String [] dizhiArr = null;
        try {
            if(isIpAddress(ip)) {
                // 根据IP搜索地址信息
                config = new DbConfig();
                searcher = new DbSearcher(config, "D:\\data\\ip2region.db");
                block = searcher.btreeSearch(ip);
                dizhi = block == null ? "" : block.getRegion();
                System.out.println(dizhi+":dizhi");
            }
            if(!dizhi.equals("")){
                dizhiArr = dizhi.split("\\|");
                dizhi = dizhiArr[index];
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            searcher = null;
            config = null;
            block = null;
        }
        return dizhi;
    }

    /**
     * 获取地址（中国|0|北京|北京市|阿里云）
     * @param ip 地址
     * */
    public static String address(String ip) {
        DataBlock block = null;
        DbSearcher searcher = null;
        DbConfig config = null;
        String dizhi = "";
        String [] dizhiArr = null;
        try {
            if(isIpAddress(ip)) {
                // 根据IP搜索地址信息
                config = new DbConfig();
                searcher = new DbSearcher(config, "D:\\data\\ip2region.db");
                block = searcher.btreeSearch(ip);
                dizhi = block == null ? "" : block.getRegion();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            searcher = null;
            config = null;
            block = null;
        }
        return dizhi;
    }

    /**
     * 获取地址（中国|0|北京|北京市|阿里云）
     * @param ip 地址
     * */
    public static String[] getAddress(String ip,String file) {
        DataBlock block = null;
        DbSearcher searcher = null;
        DbConfig config = null;
        String dizhi = "";
        String [] dizhiArr = null;
        try {
            if(isIpAddress(ip)) {
                // 根据IP搜索地址信息
                config = new DbConfig();
                searcher = new DbSearcher(config, file);
                block = searcher.btreeSearch(ip);
                dizhi = block == null ? "" : block.getRegion();
            }
            if(!"".equals(dizhi)){
                dizhiArr = dizhi.split("\\|");
                return dizhiArr;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if(searcher!=null)
                    searcher.close();//关闭流
            } catch (IOException e) {
                e.printStackTrace();
            }
            config = null;
            block = null;
        }
        return dizhiArr;
    }

}
